package com.yq.cp.controller.basics;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.entity.basics.EquipmentCategory;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.EquipmentCategoryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 设备大类-类型 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/equipmentCategory")
public class EquipmentCategoryController {
    @Resource
    EquipmentCategoryService equipmentCategoryService;

    /**
     * 设备类型列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        QueryWrapper wrapper = new QueryWrapper<EquipmentCategory>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("name",vo.getLike());
        wrapper.eq("parent_id", 0);
        List<EquipmentCategory> parentId = equipmentCategoryService.list(wrapper);
        return ApiResult.success(parentId);
    }
    /**
     * 子类型分类
     * @param vo
     * @return
     */
    @PostMapping("zhilist")
    public ApiResult zhilist(@RequestBody PageVo vo){
        QueryWrapper wrapper = new QueryWrapper<EquipmentCategory>();
            wrapper.eq("parent_id", vo.getId());
        List<EquipmentCategory> parentId = equipmentCategoryService.list(wrapper);
        EquipmentCategory byId = equipmentCategoryService.getById(vo.getId());
        for (EquipmentCategory equipmentCategory : parentId) {
            equipmentCategory.setTypeName(byId.getName());
        }
        return ApiResult.success(parentId);
    }
}

